package com.linkhub.oms.component.trace.core;

import com.linkhub.oms.component.trace.util.TraceInfoAssist;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description: trace传递对象
 * @Author: echo
 * @Date: 2022/8/31 17:26
 * @Version: 1.0
 */
@Data
public class QtTransferInfo implements Serializable {

    /**
     * requestId
     */
    private String requestId;

    /**
     * parentSpanId, 默认为1
     */
    private String parentSpanId = "1";

    /**
     * 当前 {@code spanId} 信息
     */
    private LeafSpanIdInfo leafSpanIdInfo;

    public String createTraceId(int spanId) {
        return TraceInfoAssist.getTraceId(this, spanId);
    }

    public String createTraceId() {
        return TraceInfoAssist.getTraceId(this.requestId, this.parentSpanId);
    }

    public int getAndIncrementSpanId() {
        // such as: requestId_1
        String tracePrefixKey = TraceInfoAssist.getTracePrefixKey(this);
        return this.getLeafSpanIdInfo().nextSpanId(tracePrefixKey);
    }

    @Override
    public String toString() {
        return "QtTransferInfo{" +
                "requestId='" + requestId + '\'' +
                ", parentSpanId='" + parentSpanId + '\'' +
                ", leafSpanIdInfo=" + leafSpanIdInfo +
                '}';
    }

}
